package com.qlylkj.lbk.ui.main

import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseActivity
import com.qlylkj.lbk.ui.cochain.CoChainFragment
import com.qlylkj.lbk.ui.home.HomeFragment
import com.qlylkj.lbk.ui.mine.MineFragment
import com.forest.core.etx.toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.model.event.LiveEvent
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    private val homeFragment by lazy { HomeFragment.newInstance() }

    private val cochainFragment by lazy { CoChainFragment.newInstance() }

    private val mineFragment by lazy { MineFragment.newInstance() }

    private val fragmentList by lazy { mutableListOf(homeFragment, cochainFragment, mineFragment) }

    private var selectMenuId = 0

    override fun setLayoutId() = R.layout.activity_main

    override fun initView() {
        LiveEventBus.get(LiveEvent.LOGIN_KEY, Boolean::class.java)
            .observeSticky(this, Observer<Boolean> {
                if(it && selectMenuId == R.id.cochain){
                    bottomNavigationView.selectedItemId = R.id.cochain
                }
            })
        initViewPager()
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelected)
    }

    override fun initData() {

    }

    private fun initViewPager() {
        mainViewPager.apply {
            isUserInputEnabled = false
            offscreenPageLimit = 2
            adapter = object : FragmentStateAdapter(this@MainActivity) {

                override fun getItemCount() = fragmentList.size

                override fun createFragment(position: Int) = fragmentList[position]
            }
        }
    }

    private val onNavigationItemSelected = BottomNavigationView.OnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.home -> {
                selectMenuId = R.id.home
                switchFragment(0)
            }
            R.id.cochain -> {
                selectMenuId = R.id.cochain
                if(isLoginPrompt()) {
                    switchFragment(1)
                }else{
                    return@OnNavigationItemSelectedListener false
                }
            }
            R.id.mine -> {
                selectMenuId = R.id.mine
                switchFragment(2)
            }
        }
        return@OnNavigationItemSelectedListener true
    }

    private fun switchFragment(position: Int) {
        if(position == 0){
            mImmersionBar.fitsSystemWindows(true).statusBarColor(R.color.colorPrimary).init()
        }else{
            mImmersionBar.fitsSystemWindows(true).transparentStatusBar().statusBarDarkFont(true, 0.2f).init()
        }
        mainViewPager.setCurrentItem(position, false)
    }

    private var lastTime = 0L

    override fun onBackPressed() {
        if (System.currentTimeMillis() - lastTime > 2000) {
            toast("再按一次退出程序")
            lastTime = System.currentTimeMillis()
        } else {
            finish()
        }
    }

}
